package com.fengye.algorithms.alibaba.dagscheduler;

import java.util.Objects;

/**
 * @Author fengye
 * @Date 2023/4/6 11:04
 **/
public class Task implements Executor{

    /**
     * 任务id
     */
    private Long id;
    /**
     * 任务名称
     */
    private String name;
    /**
     * 任务状态：0为执行，1，正在执行，2，已执行
     */
    private int state;

    public Task(Long id, String name, int state) {
        this.id = id;
        this.name = name;
        this.state = state;
    }

    @Override
    public boolean execute() {
        state = 1;
        System.out.println("Task[id:" + id + ", name:" + name + "] is executing.");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        state = 2;
        return true;
    }

    public boolean hasEexecuted() {
        return state == 2;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Task task = (Task) o;
        return state == task.state && id.equals(task.id) && name.equals(task.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, state);
    }
}
